﻿#include "dingdandetail.h"
#include <QJsonObject>
#include "tools.h"
#include "dbhelper.h"

DingdanDetail::DingdanDetail(QObject *parent) : QObject(parent)
{
    count = 0;
    price = 0;
    ticketIn = 0;
    ticketOut = 0;
    moneyIn = 0;
    moneyOut = 0;
    profit = 0;
    financeMake = 0;

    product = nullptr;
}

DingdanDetail::DingdanDetail(QJsonObject obj){
    DingdanDetail();
    loadFromJsonObject(obj);
}

void DingdanDetail::loadFromJsonObject(QJsonObject obj){
     id = obj.value("id").toInt();
     username = obj.value("username").toString();
     dingdanId = obj.value("dingdan_id").toString();
     productId = obj.value("product_id").toString();
     count = obj.value("count").toInt();
     price = obj.value("price").toDouble();
     ticketIn = obj.value("ticket_in").toDouble();
     ticketOut = obj.value("ticket_out").toDouble();
     moneyIn = obj.value("money_in").toDouble();
     moneyOut = obj.value("money_out").toDouble();
     profit = obj.value("profit").toDouble();

//     Tools::cout("id:"+QString("%1").arg(id));
//     Tools::cout("username:"+username);
//     Tools::cout("dingdanId:"+dingdanId);
//     Tools::cout("productId:"+productId);
//     Tools::cout("count:"+QString("%1").arg(count));
//     Tools::cout("price:"+QString("%1").arg(price));
//     Tools::cout("ticketIn:"+QString("%1").arg(ticketIn));
//     Tools::cout("ticketOut:"+QString("%1").arg(ticketOut));
//     Tools::cout("moneyIn:"+QString("%1").arg(moneyIn));
//     Tools::cout("moneyOut:"+QString("%1").arg(moneyOut));
//     Tools::cout("profit:"+QString("%1").arg(profit));
}

QString DingdanDetail::toJson(){
    QString jsonStr = "{";

    jsonStr.append("\"id\":"+QString("%1").arg(id)+",");
    jsonStr.append("\"username\":"+Tools::toJsonStr(username)+",");
    jsonStr.append("\"dingdan_id\":"+Tools::toJsonStr(dingdanId)+",");
    jsonStr.append("\"product_id\":"+Tools::toJsonStr(productId)+",");
    jsonStr.append("\"count\":"+QString("%1").arg(count)+",");
    jsonStr.append("\"price\":"+QString("%1").arg(price)+",");
    jsonStr.append("\"ticket_in\":"+QString("%1").arg(ticketIn)+",");
    jsonStr.append("\"ticket_out\":"+QString("%1").arg(ticketOut)+",");
    jsonStr.append("\"money_in\":"+QString("%1").arg(moneyIn)+",");
    jsonStr.append("\"money_out\":"+QString("%1").arg(moneyOut)+",");
    jsonStr.append("\"profit\":"+QString("%1").arg(profit)+",");
    jsonStr.append("\"finance_make\":"+QString("%1").arg(financeMake));

    jsonStr.append("}");

    return jsonStr;
}

Product* DingdanDetail::getProduct(){
    if(product == nullptr){
        DbHelper helper;
        product = helper.getProductById(productId);
    }

    return product;
}
